/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package VideoStreaming;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/**
 *
 * @author naveen
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

public class Configuration {

    private String relayhost;
    private String relayport;
    private String id;
    private static final int DEFAULT_POLL_INTERVAL = 5;
    private static final int DEFAULT_ALERT_TIMEOUT = 5000;
    private String path;     
    private int pollInterval = DEFAULT_POLL_INTERVAL;
    private Stream stream;
    byte[] state = new byte[0];
   protected static final String TALKNAME_PREFIX="JxtaTalkUserName.";
    static final String VIDEOP2P="jxme-videoserver";
    // static final String VIDEOP2P_PIPEID="urn:jxta:uuid-" +"59616261646162614E50472050325033"+"5069635368614265AD5B86696C65436105";
/////////////////////////////////////////////////////////////////////////
    static final String VIDEOP2P_PIPEID="urn:jxta:uuid-"
		+ "59616261646162614E50472050325033" +
		// "D1D1D1D1D1D1D1D1D1D1D1D1D1D1D1D104";
		"D1D1D1D1D1D141D191D191D1D1D1D1D104";

    private static final String RS_NAME="videostreaming";
    private static final int CONFIG_INDEX=1;    
     private static final int MateList=2;

    public static int getCONFIG_INDEX() {
        return CONFIG_INDEX;
    }

    public static int getMateList() {
        return MateList;
    }

    public static String getRS_NAME() {
        return RS_NAME;
    }

    public int getPollInterval() {
        return pollInterval;
    }

    public void setPollInterval(int pollInterval) {
        this.pollInterval = pollInterval;
    }

    public Stream getStream() {
        return stream;
    }

    public void setStream(Stream stream) {
        this.stream = stream;
    }
    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }


    public static int getDEFAULT_ALERT_TIMEOUT() {
        return DEFAULT_ALERT_TIMEOUT;
    }

    public static int getDEFAULT_POLL_INTERVAL() {
        return DEFAULT_POLL_INTERVAL;
    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getRelayport() {
        return relayport;
    }

    public void setRelayport(String relayport) {
        this.relayport = relayport;
    }


    public String getRelayhost() {
        return relayhost;
    }

    public void setRelayhost(String relayhost) {
        this.relayhost = relayhost;
    }

    public void readConfig() throws RecordStoreException
   {
        RecordStore rs=null;

        try {
            rs = RecordStore.openRecordStore(RS_NAME, true);
            if(rs.getNumRecords()>0){
            byte[] data = rs.getRecord(CONFIG_INDEX);
            ByteArrayInputStream bais = new ByteArrayInputStream(data);
            DataInputStream dis = new DataInputStream(bais);
            setRelayhost(dis.readUTF(dis));
            setRelayport(dis.readUTF(dis));
            setId(dis.readUTF(dis));
            setPollInterval(Integer.parseInt(dis.readUTF(dis)));
            setPath(dis.readUTF());
            int stateLen = dis.readShort();
            state = new byte[stateLen];
            dis.readFully(state);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        } finally {
               System.out.println("record store empty!!");
                if (rs != null) {
                    rs.closeRecordStore();
                }
        }
   }
   public void storeConfig()
   {
   RecordStore rs=null;
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   DataOutputStream dos = new DataOutputStream(baos);
try {
            dos.writeUTF(getRelayhost());
            dos.writeUTF(getRelayport());
            dos.writeUTF(getId());
            dos.writeUTF(Integer.toString(getPollInterval()));
            dos.writeUTF(getPath());
            dos.writeShort(state.length);
            dos.write(state);
            dos.close();
            byte[] data = baos.toByteArray();
            rs = RecordStore.openRecordStore(RS_NAME, true);
            int recordId = CONFIG_INDEX;
             try {
                rs.getRecord(recordId);
                rs.setRecord(recordId, data, 0, baos.size());
            } catch (RecordStoreException rex) {
                recordId = rs.addRecord(data, 0, baos.size());

            }
   }catch(Exception e){
}finally {
            try{
                if (rs != null) {
                    rs.closeRecordStore();
                }
                dos.close();
                baos.close();}
catch(Exception e){}

}

}




public Configuration()
{
/*
stream=new Stream(getId());
this.setPollInterval(this.getDEFAULT_POLL_INTERVAL());
*/

}


}
